//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>
#include <stdlib.h>
#include "crt0.h"

PATEXIT_DATA g_pDllExitStack = NULL;

static int _terrno = 0;

extern "C" void _assert(const char *exp, const char *f, int ln);

extern "C" int * __cdecl _errno(void)
{
    return &_terrno;
}

extern "C" wchar_t * __cdecl _wgetenv(const wchar_t *envstring)
{
    return AURA_NULL;
}

extern "C" size_t __cdecl wcstombs(char *str, const wchar_t *wcs, size_t len)
{
    size_t count = 0;
    if (wcs == AURA_NULL || str == AURA_NULL) {
        return 0;
    }
    while (*wcs && count < len) {
//        if ((unsigned int)*wcs > 127)
//            _assert("_wcstombs fault, transfer error WCHAR!\n", __FILE__, __LINE__);
        *str ++ = (char)*wcs ++;
        count ++;
    }
    *str = '\0';
    return count;
}

extern "C" int __cdecl atexit(EXIT_FUNCTION func)
{
    if (g_pDllExitStack == (PATEXIT_DATA)-1) {
        return __exeatexit(func);   /* EXE */
    }
    else {
        return __doatexit(func, &g_pDllExitStack);  /* DLL */
    }
}

//extern "C" void* operator new (int sz)
//{
//    return NULL;
//}
//
//extern "C" void operator delete (void* p)
//{
//    return ;
//}
//
//extern "C" void* operator new[] (int sz)
//{
//    return NULL;
//}
//
//extern "C" void operator delete[] (void* p)
//{
//    return ;
//}

//extern "C" int __cdecl printf(const char *,...)
//{
//    return 0;
//}
//
//extern "C" int __cdecl wprintf(const wchar_t *,...)
//{
//    return 0;
//}

extern "C" int __cdecl wcsnicmp(const wchar_t* str1, const wchar_t* str2, int count)
{
    int c1, c2;
    if (count) {
        do {
            c1 = *str1++;
            if (c1 >= 'A' && c1 <= 'Z') // convert to lower case.
                c1 -= 'A' - 'a';

            c2 = *str2++;
            if (c2 >= 'A' && c2 <= 'Z') // convert to lower case.
                c2 -= 'A' - 'a';
        } while (--count && c1 && (c1 == c2));
        return (c1 - c2);
    }
    return 0;

}

extern "C" int __cdecl sprintf(char *, const char *,...)
{
    return 0;
}
